package logic

import (
	"context"
	"errors"
	"short-video-dev/mysql"
	"strings"

	"short-video-dev/videorpc/internal/svc"
	"short-video-dev/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 评论发布
func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddRequest) (*videorpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在,请先注册")
	}

	var video mysql.VideoInfo
	err = video.VideoFindByOneId(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("视频不存在")
	}

	if strings.Contains(in.CommentContent, "垃圾") || strings.Contains(in.CommentContent, "骗人") {
		return nil, errors.New("含违规词汇，请修改")
	}
	var comment = mysql.CommentInfo{
		VideoId:        int(in.VideoId),
		UserId:         int(in.UserId),
		CommentContent: in.CommentContent,
		IsAnonymous:    int(in.IsAnonymous),
		CommentStatus:  int(in.CommentStatus),
		ReplyId:        int(in.ReplyId),
	}
	err = comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("评论失败")
	}
	return &videorpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
